--#include "data\config\boss\GodShieldBossConfig.lua" once

--[[
注册一些事件
]]
function ResisteGodShieldBossProc()
	for bossIdx, bossCfg in ipairs( GodShieldBossConfig.bossList ) do
		SceneExitDispatcher.register( bossCfg.sceneId, OnCommonSceneExit )					--注册离开场景、副本后触发的事件
	end
end


function EnterGodShieldBoss( sysarg, bossIdx )
	local bossCfg = GodShieldBossConfig.bossList[bossIdx]
	if not bossCfg then
		return
	end
	local aVar = Actor.getStaticVar(sysarg)
	if not aVar.godShieldBossState then
		aVar.godShieldBossState = {}
	end
	if aVar.godShieldBossState[bossIdx] then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GodWeapon003, ttFlyTip)
		return
	end

	if CommonEnterScence( sysarg, bossCfg, LogIds.EnterGodShieldBossFee, Lang.LogTips.EnterGodShieldBossFee ) then
		local dDvar = Actor.getDyanmicVar(sysarg)
		dDvar.enterGodShieldBossTime = System.getCurrMiniTime()

		--注册回城的延迟消息
		Actor.removeEntityScriptDelayMsg( sysarg, 17 )
		--[[最后的true表示实体死亡，消息保留]]
		Actor.postEntityScriptDelayMsg( sysarg, bossCfg.beInTime*1000, 0, 17,0,0,0,0, false, true )

		--[[有时可能活动面板没有打开成功，几秒后再打开一次
		Actor.removeEntityScriptDelayMsg( sysarg, DELAY_MSG_ACTIVITY_PANEL )
		Actor.postEntityScriptDelayMsg( sysarg, 5*1000, 0, DELAY_MSG_ACTIVITY_PANEL,0,0,0,0,false,true )
		System.broadcastTipmsgLimitLev( string.format(Lang.ScriptTips.BossHome005, Actor.getName( sysarg )), ttChatWindow, bossHomeCfg.enterLevelLimit[2])
		]]
		InitGodShieldBoss(sysarg, bossCfg)

		Actor.closeDialogs( sysarg, enLinkBossWnd )	
	end
end

function InitGodShieldBoss(sysarg, bossCfg)
	local restTime = bossCfg.beInTime
	InitOnlyButtonPanel( sysarg, 2, enRightPanelActivity_GodShieldBoss, restTime, Lang.ScriptTips.GodShieldBossName )
end


function OnGodShieldBossMob(monster, monId, sceneId, x, y)
	local bossCfg = GetGodShieldBossConfigByMonsterId( sceneId )
	if not bossCfg then
		return
	end

	--SetCommonBossKilledTime( monId, 0 )
end

function OnGodShieldBossDie(monster, sysarg, monId, lastHitKiller)
	local sceneId = Actor.getSceneId( monster )
	local bossCfg,bossIndex = GetGodShieldBossConfigByMonsterId( monId )
	if not bossCfg then
		return
	end

	--local now = System.getCurrMiniTime()
	--SetCommonBossKilledTime( monId, now )
	local aVar = Actor.getStaticVar(sysarg)
	if not aVar.godShieldBossState then
		aVar.godShieldBossState = {}
	end
	aVar.godShieldBossState[bossIndex] = 1
	SendGodShieldBossData(sysarg)
end

function GetGodShieldBossConfigByMonsterId( monId )
	for i, v in ipairs( GodShieldBossConfig.bossList ) do
		for i2,v2 in ipairs(v.monsters) do
			if v2.monsterId == monId then
				return v,i
			end
		end
	end
	return nil
end

--
function IsGodShieldBossFinish(sysarg)
	local aVar = Actor.getStaticVar(sysarg)
	if not aVar.godShieldBossState then
		return
	end
	for i,v in ipairs(GodShieldBossConfig.bossList) do
		if not aVar.godShieldBossState[i] then
			return
		end	
	end
	return true
end

--下发数据
function SendGodShieldBossData(sysarg)
	local npack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sSendGodShieldBossData)
	if npack then
		local aVar = Actor.getStaticVar(sysarg)
		if not aVar.godShieldBossState then
			aVar.godShieldBossState = {}
		end
		local nLevel =  Hero.getHeroProperty(sysarg, -1, enHeroPropGodShield)
		DataPack.writeByte(npack, nLevel > 0 and 1 or 0)
		DataPack.writeByte(npack, #GodShieldBossConfig.bossList)
		for i,v in ipairs(GodShieldBossConfig.bossList) do
			DataPack.writeByte(npack, aVar.godShieldBossState[i] or 0)
		end
		DataPack.flush(npack)
	end
end
--激活
function ActiviteGodShield(sysarg)
	local nGwLevel =  Hero.getHeroProperty(sysarg, -1, enHeroPropGodShield)
	if nGwLevel == 0 then
		HeroGodWeaponLevelUp(sysarg, 2)
		SendGodShieldBossData(sysarg)
	else
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GodWeapon005, ttFlyTip)
		return
	end
	
end
function GodShieldBossOp(sysarg, packet)
	local nOpType = DataPack.readByte(packet)
	local nParam = DataPack.readByte(packet)
	if nOpType == 1 then
		SendGodShieldBossData(sysarg)
	elseif nOpType == 2 then 
		EnterGodShieldBoss(sysarg,nParam)
	elseif nOpType == 3 then
		ActiviteGodShield(sysarg)
	end
end


ResisteGodShieldBossProc()
